<template>
  <section>
    <!--工具条-->
    <el-col :span="24"
      class="toolbar"
      style="text-align: center;">
      <el-form :inline="true"
        :model="filters">
        <el-form-item label="用户ID">
          <el-input v-model="filters.appCode"
            placeholder="查询此应用下的所有服务"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"
            @click="getSv">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <!--列表-->
    <el-table :data="publishedInfo"
      highlight-current-row
      v-loading="listLoading"
      style="width: 95%;margin:auto;">
      <el-table-column prop="appCode"
        label="应用标识">
      </el-table-column>
      <el-table-column prop="apiCode"
        label="服务标识">
      </el-table-column>
      <el-table-column prop="apiName"
        label="服务名称">
      </el-table-column>
      <el-table-column prop="originalUri"
        label="网关访问路径">
      </el-table-column>
      <el-table-column label="操作"
        width="250">
        <template slot-scope="scope">
          <el-button type="primary"
            size="mini"
            @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="info"
            v-if="scope.row.apiStatus === '1'"
            size="mini"
            @click="banService(scope.row)">禁用</el-button>
          <el-button type="success"
            v-if="scope.row.apiStatus === '0'"
            size="mini"
            @click="activeService(scope.row)">激活</el-button>
          <el-button size="mini"
            type="danger"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination layout="prev, pager, next"
      v-if="pageTotal1>10"
      @current-change="handleCurrentChange"
      style="margin:10px;text-align: center;"
      :total="pageTotal1">
    </el-pagination>
    <!--编辑界面-->
    <el-dialog title="编辑服务"
      :visible.sync="editFormVisible"
      :close-on-click-modal="false"
      class="edit-service">
      <addService :sendApiCode="sendApiCode" />
    </el-dialog>
  </section>
</template>

<script>
import {
  getService,
  forbidService,
  deletetService,
  recoverService
} from "@/api/allApi"; // 引入相应API
import addService from "./publishService";

export default {
  components: {
    addService
  },
  data() {
    return {
      filters: {
        appCode: ""
      },
      searchCode:'',
      publishedInfo: [],
      page: 1,
      pageTotal1: 10,
      listLoading: false,
      sels: [], //列表选中列
      addLoading: false,
      editFormVisible: false, //编辑界面是否显示
      sendApiCode: ""
    };
  },
  methods: {
    handleCurrentChange(val) {
      this.page = val;
      let params = {
        appCode: this.searchCode,
        currentPage: this.page,
        pageSize: 10,
        type: "0"
      };
      this.getSvtable(params);
    },
    getPublished() {
      this.listLoading = true;
      let params = {
        currentPage: 1,
        pageSize: 10,
        type: "0"
      };
      this.getSvtable(params);
    },
    banService(data) {
      this.$confirm("是否禁用此服务?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let param = {
            routeId: data.routeId + ""
          };
          forbidService(param).then(res => {
            if (res.data.code === "0") {
              this.$msgbox({
                title: "提示",
                message: "成功禁用此服务",
                callback: () => {
                  this.handleCurrentChange(this.page);
                }
              });
            } else {
              this.$message({
                message: `[${res.des}]:禁止服务失败`,
                type: "error"
              });
            }
          });
        })
        .catch(() => {});
    },
    activeService(data) {
      this.$confirm("是否激活此服务?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let param = {
            routeId: data.routeId + ""
          };
          recoverService(param).then(res => {
            if (res.data.code === "0") {
              this.$msgbox({
                title: "提示",
                message: "成功激活此服务",
                callback: () => {
                  this.handleCurrentChange(this.page);
                }
              });
            } else {
              this.$message({
                message: `[${res.des}]:激活服务失败`,
                type: "error"
              });
            }
          });
        })
        .catch(() => {});
    },
    //显示编辑界面
    handleEdit(row) {
      this.editFormVisible = true;
      this.sendApiCode = row.apiCode;
    },
    handleDelete(row) {
      this.$confirm("是否删除此服务?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let param = {
            routeId: row.routeId
          };
          deletetService(param).then(res => {
            if (res.data.code === "0") {
              this.$msgbox({
                title: "提示",
                message: "删除成功",
                callback: () => {
                  this.getPublished();
                }
              });
            } else {
              this.$message({
                message: `[${res.des}]:删除失败`,
                type: "error"
              });
            }
          });
        })
        .catch(() => {});
    },
    //查询passId
    getSv() {
      let para = {
        appCode: this.filters.appCode,
        currentPage: 1,
        pageSize: 10,
        type: "0"
      };
      this.getSvtable(para);
      this.searchCode = this.filters.appCode;
    },
    getSvtable(param) {
      this.listLoading = true;
      getService(param).then(res => {
        if (res.data.code === "0") {
          this.pageTotal1 = res.data.data.pageTotal;
          this.publishedInfo = res.data.data.pageData;
        } else {
          this.$message({
            message: `[${res.data.desc}]:查询失败`,
            type: "error"
          });
        }
        this.listLoading = false;
      });
    }
  },
  mounted() {
    this.getPublished();
  }
};
</script>

<style>
.edit-service .el-dialog {
  width: 70%;
}
</style>